<?php
/**
 * Note: *
 */
namespace App\Abstracts;

use App\Caches\Game\PayChannelCache;
use App\Exceptions\PayChannelEmptyException;
use App\Models\Game\Center\PayChannel;

abstract class PayAbstract
{
    /**
     * @var PayChannel
     */
    public $payChannel;

    /**
     * @var array
     */
    public $config;

    /**
     * 获取可用支付通道
     * @param string $channel
     * @param int $price
     * @param int $sourceType
     * @param string $gateway
     * @return PayChannel
     * @throws PayChannelEmptyException
     */
    public function getUseablePayChannel(string $channel, int $price, int $sourceType = null, string $gateway = null): PayChannel
    {
        $payChannels = PayChannelCache::getChannels($channel);
        // 打乱
        shuffle($payChannels);

        $selectChannel = null;
        foreach ($payChannels as $payChannel) {
            if (!$payChannel->pay_enabled) continue;
            // 不要测试
            if ($payChannel->sandbox) continue;
            // 渠道类型限制
            if ($payChannel->source_type && !in_array($sourceType, $payChannel->source_type)) continue;
            // 网关限制
            if ($gateway && $payChannel->gateways && !in_array($gateway, $payChannel->gateways)) continue;
            // 单次支付限额
            if ($payChannel->pay_limit && $price > $payChannel->pay_limit) continue;
            // 单日支付限额
            if ($payChannel->pay_limit_day && ($price + $payChannel->pay_day_sum) > $payChannel->pay_limit_day) continue;

            $selectChannel = $payChannel;
            break;
        }

        if (!$selectChannel) throw new PayChannelEmptyException('支付通道维护中, 请使用其他支付方式');

        return $selectChannel;
    }

    /**
     * 获取可用权益通道
     * @param string $channel
     * @param int $money
     * @param int|null $sourceType
     * @return PayChannel
     * @throws PayChannelEmptyException
     */
    public function getUseableWithdrawChannel(string $channel, int $money, int $sourceType = null): PayChannel
    {
        $payChannels = PayChannelCache::getChannels($channel);
        // 打乱
        shuffle($payChannels);

        $selectChannel = null;
        foreach ($payChannels as $payChannel) {
            if (!$payChannel->withdraw_enabled) continue;
            // 不要测试
            if ($payChannel->sandbox) continue;
            // 渠道类型限制
            if ($payChannel->source_type && !in_array($sourceType, $payChannel->source_type)) continue;
            // 单次权益限额
            if ($payChannel->withdraw_limit && $money > $payChannel->withdraw_limit) continue;
            // 单日权益限额
            if ($payChannel->withdraw_limit_day && ($money + $payChannel->withdraw_day_sum) > $payChannel->withdraw_limit_day) continue;

            $selectChannel = $payChannel;
            break;
        }
        if (!$selectChannel) throw new PayChannelEmptyException('权益通道维护中, 请使用其他权益方式');

        return $selectChannel;
    }

    /**
     * 获取配置 重写配置
     * @param PayChannel $payChannel
     * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
     */
    public function getConfig(PayChannel $payChannel): array
    {
        $config = config("pay");
        $config[$payChannel->channel]['default'] = array_merge($config[$payChannel->channel]['default'], config(sprintf('pay-channel.%s.%s', $payChannel->channel, $payChannel->appid), []));
        $config = array_merge($config, ['_force' => true]);

        $this->config = $config;
        return $config;
    }

    /**
     * 完全关闭
     */
    // public function channelOff()
    // {
    //     $this->payChannel->status = PayChannel::STATUS_OFF;
    //     $this->payChannel->save();
    // }

    /**
     * 关闭支出
     */
    public function channelWithdrawDisable()
    {
        $this->payChannel->withdraw_enabled = 0;
        $this->payChannel->save();
    }

    /**
     * 关闭收入
     */
    public function channelPayDisable()
    {
        $this->payChannel->pay_enabled = 0;
        $this->payChannel->save();
    }
}
